// TypeScript file
declare function
setScreenAction(fn: any);

class WalkingLight extends eui.Component
{
    private walingComponent:eui.Component;
    private walkingMark:egret.Shape;

    private walkData:any;
    private walkTextActionArray:any[] = [];
    private walkTextArray:any[] = [];
    private walkTextCurIndex = 0;

    public constructor()
    {
        super();


        this.walkingMark = new egret.Shape();
        this.walkingMark.x = 0;
        this.walkingMark.width = 1080;
        this.walkingMark.height = 40;
        this.walkingMark.graphics.beginFill(0x000000);
        this.walkingMark.graphics.drawRect(0,0, 1080, 40);
        this.walkingMark.graphics.endFill();
        this.walkingMark.alpha = 0.5;
        this.addChild(this.walkingMark);


        this.walingComponent = new eui.Component();
        this.walingComponent.x = 0;
        this.walingComponent.width = 1080;
        this.walingComponent.height = 40;
        this.addChild(this.walingComponent);

        this.visible = false;



    }

    public showWalking(walkData:any)
    {
        this.visible = true;
        
        if(this.walkTextArray.length > 0){
            for(let i = 0; i < this.walkTextArray.length; i++)
            {
                egret.Tween.removeTweens(this.walkTextArray[i]);
                this.walkTextArray[i].removeEventListener(egret.TouchEvent.TOUCH_TAP,this.walkTextTouch,this);
            }
        }
        this.walingComponent.removeChildren();
        this.walkData = walkData;
        this.walkTextActionArray = [];
        this.walkTextArray = [];
        this.walkTextCurIndex = 0;
        this.showText();
    }

    private showText():void{
        if ( ! this.walkData || this.walkData == "" ||  this.walkData.length == 0 )
        {
            return ;
        }

        for(let i:number = 0; i < this.walkData.length; i++)
        {
            let walkObjec = this.walkData[i];
            let text:eui.Label = new eui.Label();
            text.$setText(walkObjec.Content);
            text.name = i.toString();
            text.x = 1090;
            text.y = 10;
            text.size = 18;
            this.walingComponent.addChild(text);
            this.walkTextArray[i] = text;
            if(walkObjec.ScreenAction)
            {
                text.addEventListener(egret.TouchEvent.TOUCH_TAP,this.walkTextTouch,this);
                this.walkTextActionArray[i] = walkObjec.ScreenAction;
            }

        }

        this.startTween();
    }

    private walkTextTouch(evt:egret.Event)
    {
        let name = evt.target.name;
        if(this.walkTextActionArray[Number(name)])
        {
            setScreenAction(this.walkTextActionArray[Number(name)]);
        }
    }


    private startTween(){

        if(this.walkTextArray.length > 0){
            let text:eui.Label = this.walkTextArray[this.walkTextCurIndex];
            egret.Tween.get(text,{onChange:this.onchange,onChangeObj:this})
            .to({x:-text.width - 10,y:text.y},20000)
            .wait(0)
            .call(this.onComplete,this,[text,this.walkTextCurIndex]);
        }
        
    }

    private onchange(){        
    }

    private onComplete(curText:any,index:any)
    {
        curText.x = 1090;
        if(index >= (this.walkTextArray.length - 1))
        {
            this.walkTextCurIndex = 0;
        }else{
            this.walkTextCurIndex ++;
        }
        this.startTween();
    }

    

}